package edu.thu.studio.ormdesigner.editor.dialog.tab.view;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.TabFolder;

import edu.thu.studio.ormdesigner.common.InputException;
import edu.thu.studio.ormdesigner.editor.dialog.AbstractGeneralTabWrapper;
import edu.thu.studio.ormdesigner.editor.dialog.NodeDialog;
import edu.thu.studio.ormdesigner.model.orm.view.View;
import edu.thu.studio.ormdesigner.util.Properties;
import edu.thu.studio.ormdesigner.util.Utils;

public class GeneralTabWrapper extends AbstractGeneralTabWrapper {
	
	private View view;

	public GeneralTabWrapper(NodeDialog dialog, TabFolder parent,
			View ormView) {
		super(dialog, parent, Properties.diagram_label_table_general);
		this.view = ormView;
	}

	@Override
	protected void initLayout(GridLayout layout) {
		super.initLayout(layout);
		layout.numColumns = 2;
	}

	@Override
	protected void setData() {
		super.setData();
		if (null != view) {
			this.codeText.setText(this.view.getCode());
			this.nameText.setText(this.view.getName());
			this.commentText.setText(this.view.getComment());
			this.generateCodeText.setSelection(this.view.getGenerateCode());
			this.stereotypeText.setText(this.view.getStereotype());
		}
	}

	@Override
	public void validatePage() throws InputException {
		super.validatePage();
	}

	@Override
	public void perfomeOK() {
		// initCopyData();
	}
	
	private void initCopyData() {
		if (!Utils.isNull(this.codeText.getText().trim())) {
			this.view.setCode(this.codeText.getText().trim());
		}
		if (!Utils.isNull(this.nameText.getText().trim())) {
			this.view.setName(this.nameText.getText().trim());
		}
		this.view.setComment(this.commentText.getText().trim());
		this.view.setGenerateCode(this.generateCodeText.getSelection());
		this.view.setStereotype(this.stereotypeText.getText());
	}

	@Override
	protected boolean unqCode() {
		initCopyData();
		return Utils.isUniqueCode(this.view);
	}

	@Override
	protected boolean unqName() {
		initCopyData();
		return Utils.isUniqueName(this.view);
	}

}
